package com.example.rain.ttdt3;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import java.text.SimpleDateFormat;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
    public static final int PLAY = 1;
    public static final int PAUSE = 0;
    private MediaService.MyBinder myBinder;
    private SeekBar seekBar;
    private Handler handler;
    private Bundle bundle ;
    private Intent intent;
    private ImageView imageView;
    private boolean isPlay=false;
    private boolean isPlaying=false;
    private boolean mPlayAuto = true;
    private int mProgress;
    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar)findViewById(R.id.se);
        seekBar.setOnSeekBarChangeListener(this);
        imageView= (ImageView)findViewById(R.id.play);
        tv1 = (TextView)findViewById(R.id.begin);
        tv2 = (TextView)findViewById(R.id.end);
        intent = new Intent("myService");
        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myBinder = (MediaService.MyBinder) service;
                isPlaying=myBinder.isPlay();
                if (isPlaying) playSeekTo();

              new Thread() {
                        @Override
                        public void run() {

                                while(true){
                                    try {
                                        Thread.sleep(30);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    Message message = new Message();
                                    bundle = new Bundle();
                                    bundle.putInt("pro", myBinder.getCurrentTime());
                                    bundle.putInt("max", myBinder.getTotalTime());

                                    bundle.putString("tv1", new SimpleDateFormat("mm:ss").format(myBinder.getCurrentTime()));
                                    bundle.putString("tv2", new SimpleDateFormat("mm:ss").format(myBinder.getTotalTime()));
                                    message.setData(bundle);
                                    handler.sendMessage(message);
                                }
                            }

                    }.start();

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, BIND_AUTO_CREATE);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                seekBar.setMax(msg.getData().getInt("max"));
                seekBar.setProgress(msg.getData().getInt("pro"));
                tv1.setText(msg.getData().getString("tv1"));
                tv2.setText(msg.getData().getString("tv2"));


            }
        };

    }


    public void start(View view) {
        if(isPlay){
            pause();
            isPlaying=false;
        }
        else{
            play();
        }
    }
public  void playSeekTo(){
    seekBar.setProgress(myBinder.seekTo(myBinder.getCurrentTime()));
    imageView.setImageResource(R.drawable.dainji_20);
    isPlay=true;
}
    public void play() {

        intent.putExtra("action", PLAY);
        startService(intent);

        imageView.setImageResource(R.drawable.dainji_20);
        isPlay=true;


    }

    public void pause() {

        intent.putExtra("action",PAUSE);
        startService(intent);
        imageView.setImageResource(R.drawable.dianji_19);;
        isPlay=false;

    }



    public void pre(View view) {
    }

    public void next(View view) {
    }

    public void showLyric(View view) {
    }


    @Override
    public void onProgressChanged(   SeekBar sb,     int progress, boolean fromUser) {

        if(sb==seekBar){
            if (!mPlayAuto) {
                mProgress = progress;
                bundle.putInt("pro", mProgress);
                bundle.putString("tv1", new SimpleDateFormat("mm:ss").format(mProgress));
            }
        }

        //Log.i("lili",progress+"...");


    }

    @Override
    public void onStartTrackingTouch(SeekBar sb) {
        if(sb==seekBar){
            mPlayAuto=false;
            pause();
        }


    }

    @Override
    public void onStopTrackingTouch(SeekBar sb) {
        if (sb==seekBar){
            mPlayAuto=true;
            sb.setProgress(myBinder.seekTo(mProgress));
            imageView.setImageResource(R.drawable.dainji_20);
            isPlay=true;
        }

    }
}
